<template>
	<view style="background: #F8F8F8;">
		<view class="user-content">
			<view class="user-box">
				<view class="tit"> 用户{{isEdit==1?'编辑':'添加'}}
					<text>(带*为必填项)</text>
				</view>
				<uv-form ref="dynamicForm" :rules="dynamicRules" :model="dynamicFormData" errorType="toast" labelWidth="6em" label-align="left">
					<uv-form-item label="合作类型" prop="typeName" >
						<uv-input v-model="dynamicFormData.typeName" readonly border="none" />
					</uv-form-item>
					<uv-form-item label="首单"  prop="divide_rate">
						<uv-input type="number" readonly border="none" placeholder="请输入首单分成比例" v-model="dynamicFormData.one_divide_rate"></uv-input>
						<text>%</text>
					</uv-form-item>
					<uv-form-item label="复购"  prop="divide_rate">
						<uv-input type="number" readonly border="none" placeholder="请输入复购分成比例" v-model="dynamicFormData.two_divide_rate"></uv-input>
						<text>%</text>
					</uv-form-item>
					<uv-form-item label="手机号码" prop="mobile" required>
						<uv-input border="none" v-model="dynamicFormData.mobile" placeholder="请输入手机号码" />
					</uv-form-item>
					<uv-form-item label="账户昵称" required prop="nickname">
						<uv-input border="none" v-model="dynamicFormData.nickname" placeholder="请输入账户昵称" />
					</uv-form-item>
					<!-- 					<uv-form-item label="登录密码" prop="password">
						<uv-input type="password" border="none" placeholder="请输入登录密码"
							v-model="dynamicFormData.password"></uv-input>
					</uv-form-item> -->
					<uv-form-item label="备注" prop="remarks">
						<uv-input type="text" border="none" placeholder="请输入备注" v-model="dynamicFormData.remarks"></uv-input>
					</uv-form-item>
				</uv-form>
			</view>
			<button size="mini" @click="submit('dynamicForm')" class="btn">提交</button>
		</view>
	</view>
</template>
<script>
	import api from '@/api/index2.js';
	export default {
		data() {
			return {
				isEdit: 0,
				userinfo: '',
				// 数据源
				dynamicFormData: {
					one_divide_rate:80,
					two_divide_rate:20,
					typeName: '代理商',
					type: '1',
					mobile: '',
					remarks: '',
					nickname: '',
					password: '',
					parent_mobile: '',
					parent_divide_rate: '',
				},
				// 规则
				dynamicRules: {
					mobile: [{
						required: true,
						message: '手机号码不能为空',
						trigger: ['blur', 'change']
					}],
					nickname: [{
						required: true,
						message: '账户昵称不能为空',
						trigger: ['blur', 'change']
					}],
					// divide_rate: [{
					// 	validator: (rule, value, callback) => {
					// 		if (value >= 0 && value <= 30) {
					// 			return true;
					// 		} else {
					// 			return false;
					// 		}
					// 	},
					// 	message: '分成比例只能输入0～30',
					// 	trigger: ['blur', 'change']
					// }]
				}
			};
		},
		onLoad(query) {
			if (query.isEdit == 1) {
				this.isEdit = 1
				uni.setNavigationBarTitle({
					title: '编辑代理商'
				})
				this.dynamicFormData = JSON.parse(JSON.stringify(this.$store.state.tempUser));
				this.dynamicFormData.typeName='代理商'
				// this.dynamicFormData.one_divide_rate = 80
				// this.dynamicFormData.two_divide_rate = 20
			} else {
				const app = getApp()
				this.dynamicFormData.one_divide_rate = app.globalData.one_divide_rate
				this.dynamicFormData.two_divide_rate = app.globalData.two_divide_rate
				this.isEdit = 0
				uni.setNavigationBarTitle({
					title: '添加代理商'
				})
			}
		},
		onReady() {
			this.getUserInfo()
			this.$refs.dynamicForm.setRules(this.dynamicRules);
		},
		methods: {
			getUserInfo() {
				api.getUserInfo().then(res => {
					this.userinfo = res.data;
				});
			},
			submit(form) {
				let that = this
				uni.$uv.throttle(() => {
					that.$refs.dynamicForm.validate().then(res => {
						api.addUser({
							...that.dynamicFormData,
							is_admin: 0
						}).then(res => {
							uni.showToast({
								title: res.msg
							});
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 2000)
						});
					})
				}, 1000)
			}
		}
	};
</script>
<style lang="scss" scoped>
	.user-content {
		background: url('../../static/images/home/home-bg.png') no-repeat;
		background-size: 100% 408rpx;
		padding-bottom: 100rpx;
		padding-top: 22rpx;

		.uv-form {
			padding-bottom: 20rpx;

			.uv-form-item {
				border-bottom: 2rpx solid #d3ebff;
				margin-bottom: 12rpx;
				padding-bottom: 12rpx;

				.uv-form-item__label {
					font-size: 28rpx !important;
				}

				.uv-form-item__content {
					input {
						font-size: 28rpx;
					}
				}
			}
		}

		.user-box {
			margin: 0 22rpx;
			background: url('../../static/images/home/empty-bg.png') no-repeat;
			background-size: 100% 100%;
			padding: 60rpx 60rpx;

			.tit {
				font-family: PingFangSC, PingFang SC;
				font-weight: 550;
				font-size: 34rpx;
				color: #000000;
				line-height: 48rpx;
				font-style: normal;
				margin-bottom: 20rpx;

				uni-text {
					color: #b0b0b0;
					font-weight: 500;
					font-size: 28rpx;
				}
			}

			.check-list {
				display: flex;
				align-items: center;

				uni-image {
					width: 40rpx;
					height: 40rpx;
				}

				.check-item {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
					line-height: 40rpx;
					margin: 20rpx 0;

					uni-text {
						color: #999999;
					}
				}
			}
		}

		.btn {
			background: linear-gradient(311deg, #93a4fb 0%, #2f9afe 100%);
			box-shadow: 0rpx 4rpx 12rpx 0px rgba(20, 135, 255, 0.4);
			border-radius: 44rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 34rpx;
			color: #ffffff;
			line-height: 88rpx !important;
			text-align: right;
			font-style: normal;
			height: 88rpx;
			margin: 36rpx 28rpx;
			width: calc(100% - 56rpx);
			text-align: center;
			border: none;

			&:after {
				border: none;
			}
		}
	}
</style>